using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Collections;
using EnvDTE;

namespace XnaDesignerGenerator
{
    public class ProjectTypeConverter : CollectionConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
                return true;
            if (sourceType == typeof(Project))
                return true;
            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(Project))
                return true;
            if (destinationType == typeof(string))
                return true;
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if(value is string)
            {
                try
                {
                    DTE dte = (DTE)context.GetService(typeof(DTE));
                    foreach (object o in dte.Solution.Projects)
                    {
                        Project prj = (Project)o;
                        if (prj.Name == value.ToString() || value.ToString().StartsWith(dte.Solution.Properties.Item("Name").Value.ToString()+ "." + prj.Name))
                            return prj;
                    }
                }
                catch (Exception e)
                {
                    System.Windows.Forms.MessageBox.Show(e.Message + "\n" + e.StackTrace, e.GetType().FullName);
                }
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType==typeof(string) && value is Project)
            {
                return ((Project)value).Name;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            IList result = new ArrayList();
            DTE dte=(DTE)context.GetService(typeof(DTE));
            foreach (object o in dte.Solution.Projects)
            {
                if (o is EnvDTE.Project)
                {
                    EnvDTE.Project prj = (EnvDTE.Project)o;
                    result.Add(prj);
                }
            }
            return new StandardValuesCollection(result);
        }

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}
